// pages/login.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        //判断小程序的API，回调，参数，组件等是否在当前版本可用。
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    bindGetUserInfo: function (e) {
        if (e.detail) {
            let that = this;
            wx.login({
                success: res => {
                    app.api.login(res.code, e.detail.encryptedData, e.detail.iv).then(res => {
                        wx.setStorageSync('userToken', res.data.access_token)
                        this.getUserInfo();

                    });
                },
            });
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权，将无法登录，请授权之后再进入!!!',
                showCancel: false,
                confirmText: '返回授权',
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击了“返回授权”');
                    }
                }
            });
        }
    },

    getUserInfo: function () {
        app.api.me().then(res => {
            console.log(res.data)
            this.toIndex()
        });
    },

    toIndex: function () {
        wx.switchTab({
            url: '/pages/index/index',
        })
    }
})